﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using GISCommon.Tools;

namespace GISCommon
{
    public partial class RCITS_MapTools : UserControl
    {
        private GISCommon.Business.MapToolsHelper m_MapTools = null;//
        private AxMapControl m_pMapControl = null;

        public RCITS_MapTools()
        {
            InitializeComponent();
        }

        public void init(AxMapControl MapControl)
        {
            m_pMapControl = MapControl;
            if (m_pMapControl != null)
            {
                this.measureTools1.Initial(m_pMapControl);
                this.identityCom1.Initial(m_pMapControl);
                this.findCom1.MapControl = m_pMapControl;
                m_MapTools = new GISCommon.Business.MapToolsHelper(m_pMapControl);
                m_MapTools.CanNextView += new GISCommon.Business.MapToolsHelper.MyHandler(m_MapTools_CanNextView);
                m_MapTools.CanPreView += new GISCommon.Business.MapToolsHelper.MyHandler(m_MapTools_CanPreView);

                //pic_Scale.Image = AppResources.Res.Resources.GetImage("map_sacle_text.jpg");

                tsb_Cancel.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_Cancel_Click);

                tsb_ZoomIn.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_ZoomIn_Click);

                tsb_ZoomOut.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_ZoomOut_Click);

                tsb_Pan.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_Pan_Click);
                
                tsb_FullExtent.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_FullExtent_Click);

                tsb_PreView.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_PreView_Click);

                tsb_NextView.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_NextView_Click);

                tsb_Identity.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_Identity_Click);

                tsb_Find.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_Find_Click);

                tsb_Measure.buttonBMPClick += new SysCommon.UI.ButtonBMPClick(tsb_Measure_Click);

                Scale mapScale = new Scale();
                mapScale.init(m_pMapControl);      //地图比例显示
                mapScale.ScaleResize += new scaleResize(mapScale_ScaleResize);
            }
        }

        void mapScale_ScaleResize(ScaleEventArgs e)
        {
            this.cmb_Scale.Text = "1:" + e.ScaleValue.ToString("00000");
        }

        void m_MapTools_CanPreView()
        {
            if(m_MapTools.CanZoomToLastExtentBack)
                tsb_PreView.Enabled = true;
            else
                tsb_PreView.Enabled = false;
        }

        void m_MapTools_CanNextView()
        {
            if (m_MapTools.CanZoomToNextExtentForWord)
                tsb_NextView.Enabled = true;
            else
                tsb_NextView.Enabled = false;
        }

        private void tsb_Find_Click(object sender, EventArgs e)
        {
            measureTools1.StopMeasure();
            findCom1.Show();
        }

        private void tsb_Identity_Click(object sender, EventArgs e)
        {
            measureTools1.StopMeasure();
           if(identityclickisavailability)identityCom1.Show();
        }

        private bool identityclickisavailability = true;
        public bool IdentityClickIsAvailability
        {
            get { return identityclickisavailability;}
            set { identityclickisavailability = value;}
        }

        private void tsb_Measure_Click(object sender, EventArgs e)
        {
            measureTools1.StartMeasure();
        }

        private void tsb_ZoomIn_Click(object sender, EventArgs e)
        {
            m_MapTools.ZoomIn();
        }

        private void tsb_ZoomOut_Click(object sender, EventArgs e)
        {
            m_MapTools.ZoomOut();
        }

        private void tsb_Pan_Click(object sender, EventArgs e)
        {
            m_MapTools.Pan();
        }

        private void tsb_FullExtent_Click(object sender, EventArgs e)
        {
            m_MapTools.FullExtent();
        }

        private void tsb_PreView_Click(object sender, EventArgs e)
        {
            m_MapTools.PreView();
        }

        private void tsb_NextView_Click(object sender, EventArgs e)
        {
            m_MapTools.NextView();
        }

        private void tsb_Cancel_Click(object sender, EventArgs e)
        {
            m_MapTools.Cancel();
        }

        private void cmb_Scale_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetScale();
        }

        private void cmb_Scale_KeyDown(object sender, KeyEventArgs e)
        {
            int m_key = e.KeyValue;
            if(m_key == 13)
            {
                SetScale();
            }
        }
        private void SetScale()
        {
            try
            {
                string tmpValue = cmb_Scale.Text.Remove(0, 2);
                double ScaleValue = double.Parse(tmpValue);
                m_pMapControl.Map.MapScale = ScaleValue;
                m_pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用SetScale出错", ex);
            }
        }
    }
}
